<?php

	class TicketConverter{
		
		public static function convert($id, $usid, $active, $uid){
			$ticket = new Ticket();
			$ticket->setId($id);
			$ticket->setUsid($usid);
			$ticket->setActive(VariableUtils::getBoolean($active));
			$ticket->setSuperUser($uid);
			return $ticket;
		}
	}
	
	class TicketGetCommand implements Command{
		private $id;
		public function __construct($id){
			$this->id = $id;
		}
		
		public function doExecute($connection){
			
			$query = "select " .
			BoaConstans::TICKET_COLUMN_USID .", ".
			BoaConstans::TICKET_COLUMN_ACTIVE .", ".
			BoaConstans::TICKET_COLUMN_UID .			
			" from " . BoaConstans::TICKET_TABLE . " where " . BoaConstans::TICKET_COLUMN_ID . "=?";

			$stmt = $connection->prepare($query);
			$stmt->bind_param("i", $this->id);
			$stmt->execute();
		
			$usid =null;
			$active =null;
			$uid = null;
			
			$stmt->bind_result($usid, $active, $uid);
			$stmt->fetch();
			$result = TicketConverter::convert($this->id, $usid, $active, $uid);				
			$stmt->close();
			
			$tdfc = new TicketDescriptionFindCommand($this->id);
			$descs = $tdfc->doExecute($connection);
			$desc = null;
			foreach($descs as $desc){
				$result->add($desc);				
			}
			return $result;
		}
	}
	
	class TicketFindCommand implements Command{
		private $controls;
		
		public function __construct(TicketSearchControls $controls){
			$this->controls = $controls;
		}
		
		public function doExecute($connection){
			$result = array();
			
			$query = "select tt." .
			BoaConstans::TICKET_COLUMN_ID .", tt.".
			BoaConstans::TICKET_COLUMN_USID .", tt.".
			BoaConstans::TICKET_COLUMN_ACTIVE .", tt.".
			BoaConstans::TICKET_COLUMN_UID .			
			" from  " . BoaConstans::TICKET_TABLE . " tt"; 
			
			$where = null;
			$types = "";
			$params = array();
			if(VariableUtils::isBoolean($this->controls->isAssignSu())){
				$superUserQuery = "tt." .BoaConstans::TICKET_COLUMN_UID . " is null";
				if(VariableUtils::getBoolean($this->controls->isAssignSu())){
					$superUserQuery = "tt." .BoaConstans::TICKET_COLUMN_UID . " is not null"; 
				}
				
				$this->addToWhere($where,$superUserQuery);
			}
			
			if (VariableUtils::isBoolean($this->controls->isActive())){
				$types .="s";
				$params[] = VariableUtils::getBoolean($this->controls->isActive());
				$this->addToWhere($where, "tt." . BoaConstans::TICKET_COLUMN_ACTIVE . "=?");
			}
			
			
			if(VariableUtils::isNumber($this->controls->getSuperUserId())){
				$types .="i";
				$params[] = $this->controls->getSuperUserId();
				$this->addToWhere($where, "tt." . BoaConstans::TICKET_COLUMN_UID . "=?");
			}
			
			
			if(VariableUtils::isNumber($this->controls->getUserId())){
				$types .="i";
				$params[] = $this->controls->getUserId();
				$userServiceQuery  = "tt." . BoaConstans::TICKET_COLUMN_USID . " in(select ust." . BoaConstans::USER_SERVICE_COLUMN_ID . " from ". 
				BoaConstans::USER_SERVICE_TABLE . " ust where  ust." . BoaConstans::USER_SERVICE_COLUMN_UID . "=?)";
				$this->addToWhere($where, $userServiceQuery);
			}
			
			if(VariableUtils::isNumber($this->controls->getUserServiceId())){
				$types .="i";
				$params[] = $this->controls->getUserServiceId();
				$this->addToWhere($where, "tt." . BoaConstans::TICKET_COLUMN_USID . "=?");
			}
		
			
			if($where != null){
				$query.=" where " . $where;				
			}
			
			$stmt = $connection->prepare($query);
			
			if($types != null && $types != ""){
				$bind_param = array();
				$bind_param[] = $types;
				for($i=0;$i<sizeof($params);$i++){
					$bind_param[] = &$params[$i];
				}
				call_user_func_array(array($stmt, 'bind_param'), $bind_param);
			}
			
			$stmt->execute();
			$id=null;
			$usid =null;
			$active =null;
			$uid = null;
			
			
			$stmt->bind_result($id, $usid, $active, $uid);
			while($stmt->fetch()){
				$result[] = TicketConverter::convert($id, $usid, $active, $uid);				
			}
			
			foreach($result as $ticket){
				$tdfc = new TicketDescriptionFindCommand($ticket->getId());
				$descs = $tdfc->doExecute($connection);
				$desc = null;
				foreach($descs as $desc){
					$ticket->add($desc);				
				}
			}
			return $result;
		}
		
		
		private function addToWhere(&$where, $condition){
			if($where == null){
					$where = $condition;
			}else{
				$where.= " and " . $condition;
			}
		}
	}
	
	class TicketAddCommand implements Command{
		
		private $ticket;
		public function __construct(Ticket $ticket){
			$this->ticket = $ticket;
		}
		
		public function doExecute($connection){
			
			if(!VariableUtils::isNumber($this->ticket->getUsid())){
				return false;
			}
						
			$query = "insert into " . BoaConstans::TICKET_TABLE. "(".
			BoaConstans::TICKET_COLUMN_USID.", " .
			BoaConstans::TICKET_COLUMN_ACTIVE. ") values(?,?)";
			
			$descs = $this->ticket->getDescriptions();
			$desc = $descs[0];
			$usid = $this->ticket->getUsid();
			
			$active = true;
			$stmt = $connection->prepare($query);
			$stmt->bind_param("is", $usid, $active);
			$stmt->execute();
			$stmt->close();
			
			$ticketId = mysqli_insert_id($connection);
			if(!VariableUtils::isNumber($desc->getUserId())){
				$query = "select " . BoaConstans::USER_SERVICE_COLUMN_UID . " from " . BoaConstans::USER_SERVICE_TABLE . " where " . 
				BoaConstans::USER_SERVICE_COLUMN_ID . "=?";
				$stmt = $connection->prepare($query);
				$stmt->bind_param("i", $usid);
				$stmt->execute();
				$uid = null;
				$stmt->bind_result($uid);
				$stmt->fetch();
				$stmt->close();
				$desc->setUserId($uid);
			}
			
			$desc->setTid($ticketId);
			
			
			$tdac = new TicketDescriptionAddCommand($desc);
			$tdac->doExecute($connection);
			return $ticketId;
		}	
	}
	
	class TicketDeleteCommand implements Command{
		private $id;
		public function __construct($ticketId){
			$this->id = $ticketId;
		}
		
		public function doExecute($connection){
			if($this->id == null || !VariableUtils::isNumber($this->id)){
				return false;
			}
			
			$query = "delete from " . BoaConstans::TICKET_DESCRIPTION_TABLE . " where " .
			BoaConstans::TICKET_DESCRIPTION_COLUMN_TICKET_ID . "=?";
			$stmt = $connection->prepare($query);
			$stmt->bind_param("i", $this->id);
			$stmt->execute();
			$stmt->close();
			
			$query = "delete from " . BoaConstans::TICKET_TABLE . " where " . BoaConstans::TICKET_COLUMN_ID . "= ?";
			$stmt = $connection->prepare($query);
			$stmt->bind_param("i", $this->id);
			$stmt->execute();
			$ar = $stmt->affected_rows;
			$stmt->close();
			return $ar > 0 ? true : false;
		}	
	}
	
	class TicketUnActiveCommand implements Command{
		private $id;
		public function __construct($ticketId){
			$this->id = $ticketId;
		}
		public function doExecute($connection){
			if($this->id == null || !VariableUtils::isNumber($this->id)){
				return false;
			}
			$query = "update " . BoaConstans::TICKET_TABLE . " set " . BoaConstans::TICKET_COLUMN_ACTIVE . "=? where " .
			BoaConstans::TICKET_COLUMN_ID . "=?";
			$stmt = $connection->prepare($query);
			$active = false;
			$stmt->bind_param("ii", $active,  $this->id);
			$stmt->execute();
			return true;
		}		
	}
	
	class TicketAssignSuperUserCommand implements Command{
		private $tid;
		private $uid;
		public function __construct($ticketId, $userId){
			$this->tid = $ticketId;
			$this->uid = $userId;
		}
		
		public function doExecute($connection){
			if($this->tid == null || !VariableUtils::isNumber($this->tid) || $this->uid == null || !VariableUtils::isNumber($this->uid)){
				return false;
			}
			
			$query = "update " . BoaConstans::TICKET_TABLE . " set " .
			BoaConstans::TICKET_COLUMN_UID . "=?" .
			" where " .  BoaConstans::TICKET_COLUMN_ID . "=?";
			$stmt = $connection->prepare($query);
			$stmt->bind_param("ii", $this->uid, $this->tid);
			$stmt->execute();
			$ar = $stmt->affected_rows;
			$stmt->close();
			return $ar > 0 ? true : false;	
		}	
	}
	
	
	
	
	// ==========================================================================
	//							 TICKET DESCRIPTION COMMANDS  
	// ==========================================================================
	
	
	class TicketDescriptionConverter{
		
		public static function convert($id, $uid, $tid, $desc){
			$result = new TicketDescription();
			$result->set($desc);
			$result->setUserId($uid);
			$result->setId($id);
			$result->setTid($tid);
			return $result;
		}
	}
	
	class TicketDescriptionFindCommand implements Command{
		private $tid;
		public function __construct($tid){
			$this->tid = $tid;
		}
		
		public function doExecute($connection){
			if(!VariableUtils::isNumber($this->tid)){
				return false;
			}
			
			$result = array();
			
			$query = "select " . 
			BoaConstans::TICKET_DESCRIPTION_COLUMN_ID . ", " .  
			BoaConstans::TICKET_DESCRIPTION_COLUMN_DESC . ", ". 			
			BoaConstans::TICKET_DESCRIPTION_COLUMN_USER_ID . " from " . BoaConstans::TICKET_DESCRIPTION_TABLE . "  where "
			. BoaConstans::TICKET_DESCRIPTION_COLUMN_TICKET_ID . "=?";
			
			$stmt = $connection->prepare($query);
			$stmt->bind_param("i", $this->tid);
			$stmt->execute();
			
			$id = null;
			$desc = null;
			$uid = null;
			$stmt->bind_result($id, $desc, $uid);
			
			while($stmt->fetch()){
				$result[] = TicketDescriptionConverter::convert($id, $uid, $this->tid, $desc);
			}
			return $result;
		}
	}
	
	class TicketDescriptionModifyCommand implements Command{
		
		private $td;
		public function __construct(TicketDescription  $td){
			$this->td = $td; 
		}		
		
		public function doExecute($connection){
			if($this->td == null || !VariableUtils::isNumber($this->td->getId())){
				return false;
			}
			$query = "update " .BoaConstans::TICKET_DESCRIPTION_TABLE . " set " .
			BoaConstans::TICKET_DESCRIPTION_COLUMN_DESC . " =?  where ".
			BoaConstans::TICKET_DESCRIPTION_COLUMN_ID . " =?"; 
			$stmt = $connection->prepare($query);
			$stmt->bind_param("si", $this->td->get(), $this->td->getId());
			$stmt->execute();
			$stmt->close();
			return true;
		}
	}
	
	
	class TicketDescriptionAddCommand implements Command{
		
		private $td;
		public function __construct(TicketDescription $td){
			$this->td = $td;
		}
		public function doExecute($connection){
			if($this->td == null || $this->td->get() == null){
				return false;
			}
			
			if(!VariableUtils::isNumber($this->td->getTid()) || !VariableUtils::isNumber($this->td->getUserId())){
				return false;
			}
			
			$desc =  VariableUtils::getRealEscapeString($this->td->get());
			
			$query = "insert into " . BoaConstans::TICKET_DESCRIPTION_TABLE . "("  . 
			BoaConstans::TICKET_DESCRIPTION_COLUMN_TICKET_ID . ", " . 
			BoaConstans::TICKET_DESCRIPTION_COLUMN_USER_ID . ", " .
			BoaConstans::TICKET_DESCRIPTION_COLUMN_DESC . " ) values(?,?,?)";
			$stmt = $connection->prepare($query);
			
			$tid = $this->td->getTid();
			$uid = $this->td->getUserId();
			
			$stmt->bind_param("iis", $tid, $uid, $desc);
			$stmt->execute();
			$ar = $stmt->affected_rows;
			$stmt->close();
			return $ar > 0 ? true : false;
		}	
	}
	
	class TicketDescriptionDeleteCommand implements Command{
		private $id;
		public function __construct($id){
			$this->id = $id;
		}
		public function doExecute($connection){
			if($this->id == null || !VariableUtils::isNumber($this->id)){
				return false;
			}
			
			$query = "delete from " . BoaConstans::TICKET_DESCRIPTION_TABLE . " where " . BoaConstans::TICKET_DESCRIPTION_COLUMN_ID . "= ?";
			$stmt = $connection->prepare($query);
			$stmt->bind_param("i", $this->id);
			$stmt->execute();
			$ar = $stmt->affected_rows;
			$stmt->close();
			return $ar > 0 ? true : false;
		}	
	}
	
	
?>